package com.diamond.core.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
	
	public static final SimpleDateFormat YYYY_MM_DD_HH_MM_SS =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
    /**
     * 将时间格式化为指定的字符串
     * @param date
     * @param pattern  格式化字符串    例如 yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static String formatDate(Date date,String pattern){
    	SimpleDateFormat fmt = new SimpleDateFormat(pattern);
		return fmt.format(date);
    }

    /**
     * 获得几天后的时间
     * @param beginDate
     * @param after
     * @return
     */
	public static Date getAfterDayDate(Date beginDate, int after) {
		Calendar c = Calendar.getInstance();
		c.setTime(beginDate);
		c.add(Calendar.DAY_OF_YEAR, after);
		return c.getTime();
	}

	/**
	 * 获得多少分钟后的时间
	 * @param date
	 * @param min
	 * @return
	 */
	public static Date getAfterMinDate(Date date, int min) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.MINUTE, min);
		return c.getTime();
	}

	/**
	 * 获得从当前开始多少分钟后的时间
	 * @param min
	 * @return
	 */
	public static Date getAfterMinDate(int min) {
		return getAfterMinDate(new Date(),min);
	}

	public static Date parseDate(String dataStr, String pattern) throws ParseException {
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		return format.parse(dataStr);
	}

	public static Date getDateBegin(Date endDate) {
		Calendar c = Calendar.getInstance();
		c.setTime(endDate);
		c.set(Calendar.HOUR_OF_DAY,0);
		c.set(Calendar.MINUTE, 0);
		c.set(Calendar.SECOND,1);
		return c.getTime();
	}

	public static Date getDateEnd(Date beginDate) {
		Calendar c = Calendar.getInstance();
		c.setTime(beginDate);
		c.set(Calendar.HOUR_OF_DAY,23);
		c.set(Calendar.MINUTE, 59);
		c.set(Calendar.SECOND,59);
		return c.getTime();
	}

}
